package com.fengba.nanzhi.notice;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fengba.nanzhi.fengba.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 89311 on 2018/4/11.
 */

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private Notice notice;
    private JSONObject job;
    String title;
    String content;
    String time;


    public NoticeAdapter(Context context, Notice add) {
        this.context = context;
        this.notice = add;
    }

    @Override
    public LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.fx_notice_item_view,parent,false));
    }


    @Override
    public void onBindViewHolder(LinerViewHolder holder, int position) {
        title="";
        content="";
        try {
            job = (JSONObject) jsonArray.get(position);
             title= job.get("title").toString();
            content= job.get("content").toString();
            time = job.get("createtime").toString();
            String data= ConverToDate(time);
            holder.Ntv01.setText(title);
            holder.Ntv02.setText(content);
            holder.Ntoday01.setText(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        jsonArray = notice.getJsonArray();
        ////System.out.println("aaaaaa"+jsonArray.toString());
        if(jsonArray!=null) {
            return jsonArray.length();
        }else
            {
                return 0;
            }
    }

    public class LinerViewHolder extends RecyclerView.ViewHolder{
        private TextView Ntv01;
        private TextView Ntv02;
        private TextView Ntoday01;
        public LinerViewHolder(View itemView) {
            super(itemView);
            Ntv01 = itemView.findViewById(R.id.Ntv01);
            Ntv02 = itemView.findViewById(R.id.Ntv02);
            Ntoday01 = itemView.findViewById(R.id.Ntoday01);
        }
    }

    public final String ConverToDate(String strDate) throws Exception
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d= df.parse(strDate);
        String time = df.format(d);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new Date());
        if (time.equals(date))
        {
            DateFormat df1 = new SimpleDateFormat("hh:mm");
            Date d1= df.parse(strDate);
            String a= df1.format(d1);
            return " 今天:"+a+" ";
        }else
            {
                return " "+time+" ";
            }

    }
}
